<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Host extends CI_Controller {
	public function index(){
		$this->layout->setTitle('Host | Manage');
		$page = $this->uri->segment(3) > 1 ? $this->uri->segment(3) : 1;
		$offset = ($page - 1) * PAGE_ITEM;
		
		$sql = "UPDATE tdx_host SET status = 0 WHERE DATE_SUB(end_date,INTERVAL 20 DAY) <= '" .date('Y-m-d')."' AND end_date <= '" .date('Y-m-d')."'";
		$querysql = $this->db->query($sql);
		
		$sqlCustomer = "SELECT SQL_CALC_FOUND_ROWS *, DATE_FORMAT(end_date, '%d-%m-%Y') AS enddate, DATE_FORMAT(DATE_SUB(end_date,INTERVAL 20 DAY), '%m-%Y') AS comparedate FROM tdx_host WHERE 1=1 ORDER BY end_date ASC, host_id DESC LIMIT $offset, ".PAGE_ITEM;
		$queryCustomer = $this->db->query($sqlCustomer);
		// get total
		$sqlTotal = "SELECT FOUND_ROWS() AS found_row";
		$queryTotal = $this->db->query($sqlTotal);
		$dataTotal = $queryTotal->row();
		if($dataTotal->found_row > 0){
			if($dataTotal->found_row > PAGE_ITEM){
				$this->load->library('pagination');
				$config['base_url'] = $this->config->item('base_url').'/host/index/';
				$config['total_rows'] = $dataTotal->found_row;
				$config['per_page'] = PAGE_ITEM;
				$config['num_tag_open'] = '<li>';
				$config['num_tag_close'] = '</li>';
				$config['cur_tag_open'] = '<li><span>';
				$config['cur_tag_close'] = '</span></li>';
				$config['prev_link'] = 'Previous';
				$config['prev_tag_open'] = '<li>';
				$config['prev_tag_close'] = '</li>';
				$config['next_link'] = 'Next';
				$config['next_tag_open'] = '<li>';
				$config['next_tag_close'] = '</li>';
				$config['uri_segment'] = 3;
				$config['first_tag_open'] = '<li>';
				$config['first_tag_close'] = '</li>';
				$config['last_tag_open'] = '<li>';
				$config['last_tag_close'] = '</li>';
				
				$this->pagination->initialize($config);
				$data['paginate'] = $this->pagination->create_links();
			}
			$data['total'] = $dataTotal->found_row;
			if ($queryCustomer->num_rows() > 0){
				foreach ($queryCustomer->result_array() as $row){
					$data['items'][] = $row;
				}
			}
		}
		$this->layout->view(THEME_DIR.'/host_index', $data);
	}
	public function add(){
		$this->layout->setTitle('host | Add new host');
		$this->load->helper(array('string_func', 'string'));
		if(!empty($_POST)){
			
			if($_POST['supplier'] == ''){
				$errMsg[] = 'You must enter some data for supplier name field';
			}
			if($_POST['mail'] == ''){
				$errMsg[] = 'host email is required';
			}
			if($_POST['account'] == ''){
				$errMsg[] = 'host account is required';
			}
			if($_POST['end_date'] == ''){
				$errMsg[] = 'host date is Expires';
			}
			if($_POST['user'] == ''){
				$errMsg[] = 'User buy host is required';
			}
			
			if(empty($errMsg)){
				// check for existing customer
				
				$strSalt = generateSalt();
				$password = generatePassword($_POST['password'], $strSalt);
				$arrData = array(
					'supplier'	=> $_POST['supplier'],
					'account'	=> $_POST['account'],
					'mail'	=> $_POST['mail'],
					'user'	=> $_POST['user'],
					'domain'		=> $_POST['domain'],
					'status'	=> $_POST['status'],
					'end_date'	=> $_POST['end_date'],						
					'closed'	=> $_POST['closed'],	
				);
				if($this->db->insert('tdx_host', $arrData)){
					$data['msg'] = "New hosting has been added.";
				}
				else{
					$errMsg[] = "An error has been occured when adding new domain. Please contact our admin";
				}
			
			}
			
			if(!empty($errMsg)){
				$data['error'] = true;
				$data['error_msg'] = $errMsg;
			}
		}
		$this->layout->view(THEME_DIR.'/host_add', $data);
	}
	public function edit(){
		$this->layout->setTitle('Host | Edit host');
		$this->load->helper(array('string_func', 'string'));
		$hid = intval($this->uri->segment(3));
		$sqlcustomer = "SELECT * FROM tdx_host WHERE host_id='$hid'";
		$rscustomer = $this->db->query($sqlcustomer);
		if($rscustomer->num_rows() > 0){
			$data['hostInfo'] = $rscustomer->row_array();
			if(!empty($_POST)){
				
				if($_POST['supplier'] == ''){
					$errMsg[] = 'You must enter some data for supplier name field';
				}
				if($_POST['mail'] == ''){
					$errMsg[] = 'host email is required';
				}
				if($_POST['account'] == ''){
					$errMsg[] = 'host account is required';
				}
				if($_POST['end_date'] == ''){
					$errMsg[] = 'host date is Expires';
				}
				if($_POST['user'] == ''){
					$errMsg[] = 'User buy host is required';
				}
				
				if(empty($errMsg)){					
					$arrData = array(
						'supplier'	=> $_POST['supplier'],
						'account'	=> $_POST['account'],
						'mail'	=> $_POST['mail'],
						'user'	=> $_POST['user'],
						'domain'		=> $_POST['domain'],
						'status'	=> $_POST['status'],
						'end_date'	=> $_POST['end_date'],	
						'closed'	=> $_POST['closed'],	
					);
					
					if($this->db->update('tdx_host', $arrData, array("host_id" => $hid))){
						$data['msg'] = "Update successful.";
						$data['hostInfo'] = $arrData;
					}
					else{
						$errMsg[] = "An error has been occured when updating host. Please contact our admin";
					}
				}
				
				if(!empty($errMsg)){
					$data['error'] = true;
					$data['error_msg'] = $errMsg;
				}
			}
		}
		else{
			$data['error'] = true;
			$data['error_msg'] = array('Hosting not found');
		}
		$this->layout->view(THEME_DIR.'/host_edit', $data);
	}
	public function login()
	{
		$this->load->helper(array('string_func', 'string', 'url'));
		$result = array();
		if(isset($_POST['username'])){
			$userName = quotes_to_entities($_POST['username']);
			$query = $this->db->query("SELECT user_id, user_name, user_password, user_salt FROM tdx_users WHERE user_name='$userName' AND user_type=1");
			if ($query->num_rows() > 0){
				$row = $query->row_array();
				$password = generatePassword($_POST['password'], $row['user_salt']);
				$sqlPwd = $this->db->query("SELECT user_id, user_name, user_salt, user_email, user_sms_code 
											FROM tdx_users WHERE user_name='".$row['user_name']."' AND user_password='$password' AND user_type=1");
				if ($sqlPwd->num_rows() > 0){
					$userInfo = $sqlPwd->row_array();
					$this->session->set_userdata($userInfo);
					redirect('/home/index', 'location', 301);
				}
				else{
					$result['error'] = true;
					$result['msg'] = "Wrong username or password.";
				}
			}
			
		}
		$this->layout->setLayout('cupcake_login');
		$this->layout->setTitle('User | Login');
		$this->layout->view(THEME_DIR.'/user_login', $result);
	}
	public function logout(){
		$this->load->helper(array('url'));
		$this->session->unset_userdata(array('user_id' => '', 'user_name' => '', 'user_salt' => '', 'user_email' => '', 'user_sms_code' => ''));
		redirect('/user/login', 'location', 301);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */